package piece;

public abstract class Piece {
	public int color;
	protected int rank;
	protected int moveDistance = 1;
	
	public int getMoveDistance() {
		return moveDistance;
	}

	public final static int RANK_BOMB = 11;
	public final static int RANK_MARSHAL = 10;
	public final static int RANK_GENERAL = 9;
	public final static int RANK_COLONEL = 8;
	public final static int RANK_MAJOR = 7;
	public final static int RANK_CAPTAIN = 6;
	public final static int RANK_LIEUTENANT = 5;
	public final static int RANK_SERGEANT = 4;
	public final static int RANK_MINER = 3;
	public final static int RANK_SCOUT = 2;
	public final static int RANK_SPY = 1;
	public final static int RANK_FLAG = 0;
	public final int RANK_UNKNOWN = -1;

	public final static int COLOR_BLUE = 1;
	public final static int COLOR_RED = -1;
	
	public final static int FIGHT_ATTACKER_WIN = 1;
	public final static int FIGHT_ATTACKER_LOSE = 2;
	public final static int FIGHT_BOTH_LOOSE = 3;
	public final static int FIGHT_TAKE_FLAG = 4;
	public final static int FIGHT_TAKE_EMPTY_FIELD = 5;
	public final static int FIGHT_CANT_FIGHT = 6;
	
	/** testing whether this piece is movable
	 * @return true for most pieces
	 * 		   false for flag and mine	 
	 */
	public boolean canMove() {
		return true;
	}
	
	/** Come out the result for this move
	 * @return DEFINED RESULT TABLE
	 * Attack another piece 
	 * Usually the highest rank win
	 */
	public int attack(Piece otherPiece){
		if (this.canMove()) {
			if (otherPiece.rank == RANK_FLAG) {
				return FIGHT_TAKE_FLAG;
			} 
			else if (this.rank > otherPiece.rank) {
				return FIGHT_ATTACKER_WIN;
			} else if (this.rank < otherPiece.rank) {
				return FIGHT_ATTACKER_LOSE;
			} else return FIGHT_BOTH_LOOSE;
		}
		else return FIGHT_CANT_FIGHT;
	}
					
	
	 
	 /** Constructor
	 * 
	 * @param irank Initial value in the object
	 */
	Piece(int irank, int icolor) {
		color = icolor;
		rank = irank;		
	}
	
	/** Piece Factory Method 
	 * 
	 * @param irank
	 * @param icolor
	 * @return
	 */
	public static Piece createPiece(int irank, int icolor) {
		{
			switch (irank) {
			case RANK_BOMB: return new Bomb(irank, icolor);
			case RANK_MARSHAL: return new Marshal(irank, icolor);
			case RANK_GENERAL: return new General(irank, icolor);
			case RANK_COLONEL: return new Colonel(irank,icolor);
			case RANK_MAJOR: return new Major(irank, icolor);
			case RANK_CAPTAIN: return new Captain(irank, icolor);
			case RANK_LIEUTENANT: return new Lieutenant(irank, icolor);
			case RANK_SERGEANT : return new Sergeant(irank, icolor);
			case RANK_MINER : return new Miner(irank, icolor);
			case RANK_SCOUT: return new Scout(irank, icolor);
			case RANK_SPY: return new Spy(irank, icolor);
			case RANK_FLAG: return new Flag(irank, icolor);
			// TODO: should handle using excetption 
			default: return null;
			}
		}
	}

	public int getColor() {
		// TODO Auto-generated method stub
		return color;
	}

}
